package com.travelSpring.exception;

import com.travelSpring.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 功能：全局异常捕获器
 * 作者：卷心菜
 * 日期：2025/2/25 18:20
 */
@ControllerAdvice("com.travelSpring.controller")
public class GlobalExceptionHandler {
    private static final Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody//将Result对象转换成json格式
    public Result error(Exception e) {
        log.error("系统异常",e);
        return Result.error("系统异常");
    }

    @ExceptionHandler(UserException.class)
    @ResponseBody//将Result对象转换成json格式
    public Result UserError(UserException e) {
        return Result.error(e.getCode(),e.getMsg());
    }
}
